package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author :28169
 * @date : 2022/7/23 15:53
 */
@Component
public class ArticleListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @KafkaListener(topics = "down_or_up")
    public void getMessage(String message){
        if (StringUtils.isBlank(message)){
            return;
        }
        Map map = JSON.parseObject(message, Map.class);
        Long article = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");
        ApArticleConfig apArticleConfig = new ApArticleConfig();

        if (enable==1){
          apArticleConfig.setIsDown(false);
        }else {
            apArticleConfig.setIsDown(true);
        }
        LambdaQueryWrapper<ApArticleConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticleConfig::getArticleId,article);

apArticleConfigMapper.update(apArticleConfig,wrapper);
    }
    }


